<?php


defined('SYSPATH') or die('Access error!');

/**
 * 用户登录注册相关
 * ============================================================================
 * 功能描述： 
 *      处理用户的登录注册
 * ============================================================================
 * $Author: liuxudong $
 */
class Controller_User extends Controller_Main
{

    protected $user;

    public function before()
    {
        parent::before();
        $this->user = Model::factory('user');
        I18n::set_current_file('user');
    }

    public function action_register()
    {
        if ($this->user->logged_in())
        {
            $this->request->redirect('user/base/index');
        }
        //当前位置
        $this->set_define_position(__('register'), URL::site('user/register'));
        if ($_POST)
        {
            if (!$this->valid_post($_POST))
            {
                $this->request->redirect('user/register');
            }
            $data = array();
            $data['username'] = Html::get_safe_content(Arr::get($_POST, 'username', ''));
            $data['safecode'] = Text::random('alnum', 5);
            $data['password'] = Arr::get($_POST, 'password', '');
            $data['email'] = Html::get_safe_content(Arr::get($_POST, 'email', ''));
            $data['province'] = intval(Arr::get($_POST, 'province', 0));
            $data['city'] = intval(Arr::get($_POST, 'city', 0));
            $data['sex'] = intval(Arr::get($_POST, 'sex', 1));

            if (strlen($data['username']) < 6 || strlen($data['username']) > 20)
            {
                Message::error(__('username no existed'));
                $this->request->redirect('user/register');
            }

            if (strlen($data['password']) < 6 || strlen($data['password']) > 20)
            {
                Message::error(__('password error'));
                $this->request->redirect('user/register');
            }

            // 判断email是否格式正确
            if (!Text::is_email($data['email']))
            {
                Message::error(__('email is error'));
                $this->request->redirect('user/register');
            }
            //判断数据库是否已经存在该用户
            if ($this->user->is_exist(array('username' => $data['username'])))
            {
                Message::error(__('username existed'));
                $this->request->redirect('user/register');
            }

            //判断数据库是否存在email
            if ($this->user->is_exist(array('email' => $data['email'])))
            {
                Message::error(__('email existed'));
                $this->request->redirect('user/register');
            }
            $user_id = $this->user->insert_user($data);
            if ($user_id)
            {
                //赠送积分
                $this->save_points($user_id, 'user_register_add_points');


                //赠送经验
                $this->save_experience($user_id, 'user_q2a_ask_experience');

                //发送站内信
                $message = array();
                $message['title'] = '恭喜您成功注册氿上网';
                $message['content'] = '恭喜您成功注册氿上网';
                $message['to_user'] = $user_id;
                $this->send_message($message);

                Message::success(__('user add success'));
                $this->request->redirect('user/register');
            }
        }
        //获取地区分类
        $area = $this->get_area_cache_file();
        $this->template->assign('province', $area['province']);
        $this->template->view('register');
    }

    public function action_login()
    {
        if ($this->user->logged_in())
        {
            $this->request->redirect('user/base/index');
        }
        if ($_POST)
        {
            if (!$this->valid_post($_POST))
            {
                $this->request->redirect('user/login');
            }
            $data = array();
            $data['username'] = Arr::get($_POST, 'username', '');
            $data['password'] = Arr::get($_POST, 'password', '');
            $data['remember'] = Arr::get($_POST, 'remember', '');
            $data['redirect'] = Arr::get($_POST, 'redirect', 'user/base/index');
            if ($data['username'] == '')
            {
                Message::error(__('username no existed'));
                $this->request->redirect('user/login');
            }
            if ($data['password'] == '')
            {
                Message::error(__('password error'));
                $this->request->redirect('user/login');
            }
            //判断数据库是否已经存在该用户
            if (!$this->user->is_exist(array('username' => $data['username'])))
            {
                Message::error($data['username'] . __('username no existed'));
                $this->request->redirect('user/login');
            }

            $login_flag = $this->user->check_user($data['username'], $data['password']);

            if ($login_flag === TRUE)
            {
                $data['user_id'] = $this->user->login($data);
                if (!empty($data['remember']))
                {
                    //cookie记录
                    $this->user->cookie_login($data);
                }
                Message::success(__('login success'));
                $this->request->redirect($data['redirect']);
            }
            elseif ($login_flag === FALSE)
            {
                Message::error(__('username and password not right'));
                $this->request->redirect('user/login');
            }
        }
        //当前位置
        $this->set_define_position(__('login'), URL::site('user/login'));
        if (!empty($_GET['redirect_url']))
        {
            $referrer = $_GET['redirect_url'];
        }
        elseif (Request::$referrer != NULL)
        {
            $referrer = Request::$referrer;
        }
        $this->template->assign('redirect', $referrer);
        $this->template->view('login');
    }

    public function action_logout()
    {
        if ($this->user->logged_in())
        {
            Session::instance()->delete('user');
            Cookie::delete('JSW_USER');
            Message::success('退出成功');
            $this->request->redirect('user/login/');
        }
        $this->request->redirect('');
    }

}